package com.health.db.dao.msg.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;

import com.health.db.dao.ServiceInterface;
import com.health.db.dao.msg.IImMsgNoticeDao;
import com.health.db.mapper.ImMsgNoticeMapper;
import com.health.db.model.ImMsgNotice;
import com.health.db.model.ImMsgNoticeExample;
import com.health.db.model.RegisterOrder;
import com.health.redis.RedisConstants;

/**
 * 短信接口 未接入缓存认证
 * @author YC
 *
 */
public class ImMsgNoticeDaoImpl extends ServiceInterface implements IImMsgNoticeDao {

    private ImMsgNoticeMapper imMsgNoticeMapper;

    private String  selectByNoticeReceiveAddress=":noticeReceiveAddress:";
    private String  selectByIsUsedFlag=":isUsedFlag:";
	@Override
	public int insert(ImMsgNoticeExample record) {
		
		int result=-1;
		getRedisOperatiorLogic();
		 
		String key=RedisConstants.ImMsgNoticeMapper+selectByNoticeReceiveAddress+record.getNoticeReceiveAddress()
				+selectByIsUsedFlag+record.getIsUsedFlag();
		
		List<ImMsgNotice>  imMsgNoticeList =null;
		 Object object=null;
		 result= imMsgNoticeMapper.insert(record);

		Integer count=0;
		getRedisOperatiorLogic();
		 
		String keys=RedisConstants.ImMsgNoticeMapper+selectByNoticeReceiveAddress+record.getUuId();
		
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object )
		{
			if (!"".equals(object)) {
				count= (Integer) object;
			}
			redisOperatiorLogic.setEx(keys,86400,count+1, getOrderRedisGroupNo("1"));
		}else{
			redisOperatiorLogic.setEx(keys,86400,1, getOrderRedisGroupNo("1"));
		}
		// TODO Auto-generated method stub
		return result;
	}

	@Override
	public List<ImMsgNotice> selectByExample(ImMsgNoticeExample example) {
		// TODO Auto-generated method stub
		List<ImMsgNotice>  imMsgNoticeList =null;
		imMsgNoticeList  = imMsgNoticeMapper.selectByExample(example);
		/*getRedisOperatiorLogic();
		 
		String key=RedisConstants.ImMsgNoticeMapper+selectByNoticeReceiveAddress+example.getNoticeReceiveAddress()
				+selectByIsUsedFlag+example.getIsUsedFlag();
		
		
		 Object object=null;
		 
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			
			object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
			
		}
		
		if(null!=object && !"".equals(object))
		{
			imMsgNoticeList=(List<ImMsgNotice>) object;
		}
		else
		{
			imMsgNoticeList  = imMsgNoticeMapper.selectByExample(example);
			
			if(redisOperatiorLogic.isJedisIsConnected())
			{
				
				redisOperatiorLogic.setEx(key, imMsgNoticeList, getOrderRedisGroupNo("1"));
			}
		}*/

		
		return imMsgNoticeList;
	}

	@Override
	public int updateByPrimaryKeySelective(ImMsgNotice record) {
		// TODO Auto-generated method stub
		return imMsgNoticeMapper.updateByPrimaryKeySelective(record);
	}

	/**
	 * @return the imMsgNoticeMapper
	 */
	public ImMsgNoticeMapper getImMsgNoticeMapper() {
		return imMsgNoticeMapper;
	}

	/**
	 * @param imMsgNoticeMapper the imMsgNoticeMapper to set
	 */
	public void setImMsgNoticeMapper(ImMsgNoticeMapper imMsgNoticeMapper) {
		this.imMsgNoticeMapper = imMsgNoticeMapper;
	}

	@Override
	public int updateForUserd(ImMsgNotice record) {
		// TODO Auto-generated method stub
		
		int result=-1;
		getRedisOperatiorLogic();
		 
		String key=RedisConstants.ImMsgNoticeMapper+selectByNoticeReceiveAddress+record.getNoticeReceiveAddress()
				+selectByIsUsedFlag+record.getIsUsedFlag();
		
		List<ImMsgNotice>  imMsgNoticeList =null;
		 Object object=null;
	
		result= imMsgNoticeMapper.updateForUserd(record);
				 
	/*	if(redisOperatiorLogic.isJedisIsConnected())
		{
					//5 min
			redisOperatiorLogic.setEx(key, 5*60,record, getOrderRedisGroupNo("1"));
		}
		// TODO Auto-generated method stub
*/		return result;
	 
	}

	@Override
	public int countNotice(ImMsgNoticeExample example) {
		// TODO Auto-generated method stub
		
		Integer count=0;
		getRedisOperatiorLogic();
		 
		String key=RedisConstants.ImMsgNoticeMapper+example.getUuId();
		
		
		Object object=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object=redisOperatiorLogic.readObject(key, getOrderRedisGroupNo("1"));
		}
		if(null!=object )
		{
			if (!"".equals(object)) {
				count= (Integer) object;
			}
			if (count<=5) {
				redisOperatiorLogic.setEx(key,86400,count+1, getOrderRedisGroupNo("1"));
			}
			
		}else{
			redisOperatiorLogic.setEx(key,86400,1, getOrderRedisGroupNo("1"));
		}
		// TODO Auto-generated method stub
		return count.intValue();
	}

	@Override
	public boolean checkNotice(ImMsgNoticeExample example) {
		// TODO Auto-generated method stub
		
		Integer count1=0;
		getRedisOperatiorLogic();
		 
		String key1=RedisConstants.ImMsgNoticeMapper+example.getUuId();
		Object object1=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object1=redisOperatiorLogic.readObject(key1, getOrderRedisGroupNo("1"));
		}
		if(null!=object1 )
		{
			if (!"".equals(object1)) {
				count1= (Integer) object1;
			}
			if (count1<=5) {
				redisOperatiorLogic.setEx(key1,86400,count1+1, getOrderRedisGroupNo("1"));
			}
			
		}else{
			redisOperatiorLogic.setEx(key1,86400,1, getOrderRedisGroupNo("1"));
		}		
		
		
		
		if (count1>5)  return false;
		
		
		
		//IOS未接入sign
		if (!example.getSign().equals("2976b2f3db9530e0de8097c1c912550f")) {
		
		Integer count2=0;
		String key2=RedisConstants.ImMsgNoticeMapper+example.getSign();
		Object object2=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object2=redisOperatiorLogic.readObject(key2, getOrderRedisGroupNo("1"));
		}
		if(null!=object2 )
		{
			if (!"".equals(object2)) {
				count2= (Integer) object2;
			}
			if (count2<=5) {
				redisOperatiorLogic.setEx(key2,86400,count2+1, getOrderRedisGroupNo("1"));
			}
			
		}else{
			redisOperatiorLogic.setEx(key2,86400,1, getOrderRedisGroupNo("1"));
		}		
		
		if (count2>5)  return false;
		}
		
		
	/*	Integer count3=0;
		String key3=RedisConstants.ImMsgNoticeMapper+example.getIp();
		Object object3=null;
		
		if(redisOperatiorLogic.isJedisIsConnected())
		{
			object3=redisOperatiorLogic.readObject(key3, getOrderRedisGroupNo("1"));
		}
		if(null!=object3 )
		{
			if (!"".equals(object3)) {
				count3= (Integer) object3;
			}
			if (count3<=5) {
				redisOperatiorLogic.setEx(key3,86400,count3+1, getOrderRedisGroupNo("1"));
			}
			
		}else{
			redisOperatiorLogic.setEx(key3,86400,1, getOrderRedisGroupNo("1"));
		}		
		
		if (count3>5)  return false;*/
		
		
		return true;
	}

	
 
}
